package com.example.demo.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import com.example.demo.model.Trade;

public interface TradeRepository extends JpaRepository<Trade, Long>, JpaSpecificationExecutor<Trade> {

	@SuppressWarnings("serial")
	public default Page<Trade> pageCriteria(Trade trade, Pageable pageable) {
		Page<Trade> page = this.findAll(new Specification<Trade>() {
			@Override
			public Predicate toPredicate(Root<Trade> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (trade.getUserID() != null) {
					list.add(cb.equal(root.<Long> get("userID"), trade.getUserID()));
				}
				if (trade.getStatus() != null) {
					list.add(cb.equal(root.<Integer> get("status"), trade.getStatus()));
				}
				if (trade.getAfterSale() != null) {
					list.add(cb.equal(root.<Integer> get("afterSale"), trade.getAfterSale()));
				}
				cb.desc(root.<Long> get("tID"));

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}

		}, pageable);
		return page;
	}

	@Query("select new Map(t.tradeID as tradeID,t.status as status,t.createTime as createTime,"
			+ "t.payTime as payTime,t.endTime as endTime,t.modifyTime as modifyTime,"
			+ "t.shippingTime as shippingTime,t.totalFee as totalFee,t.payment as payment,"
			+ "t.discountFee as discountFee,t.receiverName as receiverName,t.receiverProvince as receiverProvince,"
			+ "t.receiverCity as receiverCity,t.receiverArea as receiverArea,t.receiverAddress as receiverAddress"
			+ ") from Trade t left join Order o on t.tradeID = o.tradeID where t.userID = ?1")
	public Page<Map<String, Object>> pageTradeByUserId(Long userId, Pageable pageable);

	@Query("from Trade t where t.tID = ?1")
	public Trade getByID(Long tID);

	@Query("from Trade t where t.tradeID = ?1")
	public Trade getByTradeID(String tradeID);

	@Query("select new Map(t.tradeID as tradeID, t.postFee as postFee, t.payment as payment, t.discount as discount, t.discountPrice as discountPrice, "
			+ "t.receiverName as receiverName, t.receiverProvince as receiverProvince, t.receiverCity as receiverCity,t.status as status, "
			+ "t.receiverArea as receiverArea, t.receiverAddress as receiverAddress, t.buyerMessage as buyerMessage, l.logisticsNumber as logisticsNumber, "
			+ "l.express as express, o.price*o.size as cPrice, o.size as size, o.skuTitle as skuTitle ) "
			+ "from Order o LEFT JOIN Trade t ON t.tradeID=o.tradeID LEFT JOIN Logistics l ON o.tradeID=l.tradeID where o.oID = ?1 ")
	public Map<String, Object> getTradeOrder(Long oID);

}
